package com.kuaimacode.kframework.admin.controller.role;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.role.SysAgentRoleService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 角色信息
 */
@Controller
@RequestMapping({"/agentRole"})
public class AgentRoleController {
	private static String PAGE_FILE_NAME = "agent/role/";

	@Autowired
	private SysAgentRoleService sysRoleService;

	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
						@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
						ModelMap modelMap) {
		PageInfo<SysAgentRole> pageInfo = sysRoleService.findAll(pageNum, pageSize);
		modelMap.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		SysAgentRole role = new SysAgentRole();
		role.setIsAvailable(true);
		map.put("role", role);
		map.put("isAdd", true);
		return PAGE_FILE_NAME + "form";
	}


	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		SysAgentRole role = sysRoleService.getById(id);
		map.put("role", role);
		map.put("isAdd", false);
		return PAGE_FILE_NAME + "form";
	}


	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(SysAgentRole role,Boolean isAdd, ModelMap map) {
		try {
			if(isAdd){
				// 新增角色
				sysRoleService.createRole(role);
			}else{
				// 编辑保存角色
				sysRoleService.updateById(role);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		
		//检查是否系统管理员，是则不能删除
		SysAgentRole role = sysRoleService.getById(id);
		if(!role.getIsCustom()) {
			return JsonResult.failure("该角色为系统默认角色，不能删除！");
		}
		
		//检查是否还有归属的用户，是则不能删除
		
		try {
			sysRoleService.removeById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

	@RequestMapping(value = "/grant/{id}", method = RequestMethod.GET)
	public String grant(@PathVariable String id, ModelMap map) {
		SysAgentRole role = sysRoleService.getById(id);
		map.put("role", role);
		return PAGE_FILE_NAME + "grant";
	}

	@RequestMapping(value = "/grant/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult grant(@PathVariable String id,
							@RequestParam(required = false) String[] permissionIds, ModelMap map) {
		
		try {
			
			//删除全部
			sysRoleService.removeAllPermissionByUserId(id);
			
			if(permissionIds.length>0) {
				sysRoleService.grant(id, permissionIds);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

}


